<?php
class TeacherLogServer {
	public function paging($where=array(),$numberEachPage = 10){

		$count = HttpDaoManager::getTeacherLogDao()->searchNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getTeacherLogDao()->searchList($where,'',$limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}


	public function pagingGroup($where=array(),$numberEachPage = 10){
	
		$count = HttpDaoManager::getTeacherLogDao()->searchGroupNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getTeacherLogDao()->searchGroupList($where,'',$limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	
	public function pagingOrder($where=array(),$numberEachPage = 10){

		$count = HttpDaoManager::getTeacherLogDao()->searchOrderNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getTeacherLogDao()->searchOrderList($where,'',$limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	
	public function pagingBookCourseLog($where=array(),$numberEachPage = 10){

		$count = HttpDaoManager::getTeacherLogDao()->searchBookCourseLogNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getTeacherLogDao()->searchBookCourseLogList($where,'',$limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	
	public function getTeacherSalaryStartTime(){
		$start_time = date('Y-m').'-01';
		$day = date('d');
		if($day>16){
			$start_time = date('Y-m').'-15';
		}
		return $start_time;
	}
	public function getTeacherSalaryEndTime(){
		$end_time = date('Y-m-d');
		return $end_time;
	}
	
	/**
	 *上课 
	 */
	public function haveClassWage($bookCourseId,$adminName){
		$sql = ClassManager::getSql();
// 		$sql::$showSql=1;
		$bookCourseData = HttpDaoManager::getBookCourseDao()->get($bookCourseId);
		
		//基本工资结算:基本工资+奖金+加班费
		$this->subWage($bookCourseData, $adminName);
		$this->substitute($bookCourseData, $adminName);
		$this->workOvertime($bookCourseData, $adminName);
		$this->holiday($bookCourseData, $adminName);
	}
	
	//扣工资部分、或者代课奖励，需要人工核定部分
	public function lossClassWage($bookCourseId,$adminName){
		$sql = ClassManager::getSql();
		$bookCourseData = HttpDaoManager::getBookCourseDao()->get($bookCourseId);
		if($bookCourseData['report_status']==BOOK_REPORT_STATUS_3){
			//基本工资反冲
			if($bookCourseData['class_status']==CLASS_STATUS_2){//已经完成，才反冲工资
				$teacherData  = HttpDaoManager::getTeacherDao()->get($bookCourseData['teacher_id']);
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'WAGE','type'=>$teacherData['contract_category'],'star'=>$teacherData['real_education_experience']));
				$money  = $sysTeacherSalaryData['per_salary'];
				$integral = TEACHER_INTEGRAL_STAND;
				
				if ($bookCourseData['user_status'] ==BOOK_USER_STATUS_2){//若学生缺课
					$money = $money/2;
					$data['remark'] ='student didn`t come';
					$integral = 0;
				}
				
				//正常工资结算
				$data['target_id'] =   $bookCourseId;
				$data['add_admin'] = $adminName;
				$data['type'] = 'ABSENT_RECOI';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				$data['money'] = -$money;
				$data['integral'] = $integral;
				$this->add($data);//插入日志表,获得金钱
			}
		}
		$this->absent($bookCourseData, $adminName);
		$this->late($bookCourseData, $adminName);
	}
	
	//扣工资部分、或者代课奖励，需要人工核定部分
	public function reCacluteClassWage($bookCourseId,$adminName){
		$sql = ClassManager::getSql();
		$bookCourseData = HttpDaoManager::getBookCourseDao()->get($bookCourseId);
		HttpDaoManager::getTeacherLogDao()->deleteByBookCourseId($bookCourseId);
		if($bookCourseData['report_status']==BOOK_REPORT_STATUS_3){
			$this->absent($bookCourseData, $adminName);
		}
		if($bookCourseData['report_status']==BOOK_REPORT_STATUS_2){
			$this->subWage($bookCourseData, $adminName);//基本工资
			$this->substitute($bookCourseData, $adminName);//代课
			$this->workOvertime($bookCourseData, $adminName);//加班费
			$this->holiday($bookCourseData, $adminName);//假日
			$this->late($bookCourseData, $adminName);//迟到扣除
			//正常给工资，迟到扣除
		}
		if($bookCourseData['report_status']==BOOK_REPORT_STATUS_1){
			//完全正常给工资
			$this->subWage($bookCourseData, $adminName);//基本工资
			$this->substitute($bookCourseData, $adminName);//代课
			$this->workOvertime($bookCourseData, $adminName);//加班费
			$this->holiday($bookCourseData, $adminName);//假日			
		}
	}	
	/**
	 *添加 
	 */
	public function add($data){
		$data['add_time'] =time();
		HttpDaoManager::getTeacherLogDao()->add($data);
	}
	
	/**
	 * 双休日 但不是假日奖金,老师不缺课
	 */
	public function workOvertime($bookCourseData,$adminName){
		$holiday = Common::_t('holiday');//双休日奖励
		if (date('w',$bookCourseData['book_date'])==6 || date('w',$bookCourseData['book_date'])==0 ){
			if (!in_array(date('m-d',$bookCourseData['book_date']), $holiday)&&$bookCourseData['report_status']!=BOOK_REPORT_STATUS_3){
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'WORK_OVERTIME'));
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'WORK_OVERTIME';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				if ($bookCourseData['user_status'] ==BOOK_USER_STATUS_2){
					$data['money'] = $sysTeacherSalaryData['per_salary']/2;
				}else{
					$data['money'] = $sysTeacherSalaryData['per_salary'];
				}				
				$this->add($data);
			}
		}
	}
	
	/**
	 *体验课后购买 
	 */
	public function payReward($userId,$adminName){ 
		$bookCourseData = HttpDaoManager::getBookCourseDao()->searchData(array('user_id'=>$userId,'class_status'=>CLASS_STATUS_2,'book_type'=>BOOK_TYPE_2));
		if ($bookCourseData['user_status']==BOOK_USER_STATUS_1){//体验课奖励
			$teacherLogData = HttpDaoManager::getTeacherLogDao()->searchData(array('type'=>'PAY_REWARD','target_id'=>$bookCourseData['id']));
			if (empty($teacherLogData)){
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'PAY_REWARD';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'PAY_REWARD'));
				$data['money'] = $sysTeacherSalaryData['per_salary'];
				$this->add($data);
			}
		}
	}
	
	/**
	 *代课 
	 */
	public function subWage($bookCourseData,$adminName){
		$teacherData  = HttpDaoManager::getTeacherDao()->get($bookCourseData['teacher_id']);
		$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'WAGE','type'=>$teacherData['contract_category'],'star'=>$teacherData['real_education_experience']));
		$money  = $sysTeacherSalaryData['per_salary'];
		$integral = TEACHER_INTEGRAL_STAND;
		
		if ($bookCourseData['user_status'] ==BOOK_USER_STATUS_2){//若学生缺课
			$money = $money/2;
			$data['remark'] ='student didn`t come';
			$integral = 0;
		}
		
		//正常工资结算
		$data['target_id'] =   $bookCourseData['id'];
		$data['add_admin'] = $adminName;
		$data['type'] = 'WAGE';
		$data['teacher_id']  = $bookCourseData['teacher_id'] ;
		$data['money'] = $money;
		$data['integral'] = $integral;
		$this->add($data);//插入日志表,获得金钱
	}
	/**
	 *代课 
	 */
	public function substitute($bookCourseData,$adminName){
		
		if ($bookCourseData['substitute']==BOOK_SUBSTITUTE_2||$bookCourseData['substitute']==BOOK_SUBSTITUTE_3){//对当前老师进行代课奖励
			$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'SUBSTITUTE'));
			$data['target_id'] =   $bookCourseData['id'];
			$data['add_admin'] = $adminName;
			$data['type'] = 'SUBSTITUTE';
			$data['teacher_id']  = $bookCourseData['teacher_id'] ;
			$data['money'] = $sysTeacherSalaryData['per_salary'];
			$data['remark'] = $sysTeacherSalaryData['name'];
			$this->add($data);
			
			//原来老师缺课,罚款
			if ($bookCourseData['original_teacher_id']==BOOK_SUBSTITUTE_2){//请假了则不扣工资
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'ABSENT'));
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'ABSENT';
				$data['teacher_id']  = $bookCourseData['original_teacher_id'] ;
				$data['money'] = $sysTeacherSalaryData['per_salary'];
				$data['remark'] = $sysTeacherSalaryData['name'];
					
				$this->add($data);
			}		
		}

	}
	
	/**
	 *缺席老师 ,对缺课老师进行处罚
	 */
	public function absent($bookCourseData,$adminName){
		//当前老师缺课,若已经完成单子(假完成),则需要对原先得到的进行反冲处理，并且罚款
		if ($bookCourseData['report_status']==BOOK_REPORT_STATUS_3){//当前老师缺课
			$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'ABSENT'));//
			$data['target_id'] =   $bookCourseData['id'];
			$data['add_admin'] = $adminName;
			$data['type'] = 'ABSENT';
			$data['teacher_id']  = $bookCourseData['teacher_id'] ;
			$data['money'] = $sysTeacherSalaryData['per_salary'];
			$data['remark'] = $sysTeacherSalaryData['name'];
			$data['integral'] = $sysTeacherSalaryData['integral'];
			$this->add($data);
		}
	}
	
	/**
	 *迟到 
	 */
	public function late($bookCourseData,$adminName){
		if ($bookCourseData['report_status']===BOOK_REPORT_STATUS_2){//当前老师迟到:该得到的钱还得到，要另外扣钱
			$lateNumber = HttpServerManager::getTeacherStartCourseServer()->getLateNumber($bookCourseData['teacher_id']);
			if ($lateNumber<=2){
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'LATE'));
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'LATE';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				$data['money'] = $sysTeacherSalaryData['per_salary'];
				$data['remark'] = $sysTeacherSalaryData['name'];
				$data['integral'] = $sysTeacherSalaryData['integral'];
			}else{
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'LATE';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				$teacherData  = HttpDaoManager::getTeacherDao()->get($bookCourseData['teacher_id']);
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'WAGE','type'=>$teacherData['contract_category'],'star'=>$teacherData['education_experience']));
				$data['money'] =$sysTeacherSalaryData['per_salary'];
				$data['remark'] = $sysTeacherSalaryData['name'];
				$data['integral'] = $sysTeacherSalaryData['integral'];
			}
			$this->add($data);
		}
	}
	
	/**
	 * 档案填晚了   审核后加
	 */
	public function lateArchives($courseRecordData,$adminName){
		$bookCourseData  = HttpDaoManager::getBookCourseDao()->get($courseRecordData['book_course_id']);
		$emdTime  = $bookCourseData['book_date']+122400; 
		if ($courseRecordData['complete_time']>$emdTime){
			$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'TIMEOUT_FEEDBACK'));
			$data['target_id'] =   $courseRecordData['id'];
			$data['add_admin'] = $adminName;
			$data['type'] = 'TIMEOUT_FEEDBACK';
			$data['teacher_id']  = $bookCourseData['teacher_id'] ;
			$data['money'] = $sysTeacherSalaryData['per_salary'];
		}
	}
	/**
	 *评价 
	 */
	public function evaluation($BookCourseEvaluationData,$adminName){
		$bookCourseData = HttpDaoManager::getBookCourseDao()->get($BookCourseEvaluationData['book_course_id']);
		$data['target_id'] =   $BookCourseEvaluationData['id'];
		$data['add_admin'] = $adminName;
		$data['type'] = 'EVALUATION';
		$data['teacher_id']  = $bookCourseData['teacher_id'] ;
		$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'EVALUATION','star'=>$BookCourseEvaluationData['satisfaction']));
		$data['integral']  = $sysTeacherSalaryData['integral'];
	}
	
	/**
	 *节假日奖金：反冲
	 */
	public function recoiHoliday($bookCourseData,$adminName){
		$holiday = Common::_t('holiday');
		if (in_array(date('m-d',$bookCourseData['book_date']), $holiday)){
			if ($bookCourseData['report_status']==BOOK_REPORT_STATUS_1){
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'HOLIDAY'));
				$money  = $sysTeacherSalaryData['per_salary'];
				$integral  = $sysTeacherSalaryData['integral'];
				if ($bookCourseData['user_status'] ==BOOK_USER_STATUS_2){
					$money = $money/2;
					$data['remark'] ='studentsDidnotCome';
				}
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'HOLIDAY';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				$data['money'] = -$money;
				$data['integral'] = -$integral;
				$this->add($data);
			}
		}
	}
	/**
	 *节假日奖金
	 */
	public function holiday($bookCourseData,$adminName){
		$holiday = Common::_t('holiday');
		if (in_array(date('m-d',$bookCourseData['book_date']), $holiday)){
			if ($bookCourseData['report_status']==BOOK_REPORT_STATUS_1){
				$sysTeacherSalaryData = HttpDaoManager::getSysTeacherSalaryDao()->getData(array('key'=>'HOLIDAY_SEASON'));
				$money  = $sysTeacherSalaryData['per_salary'];
				$integral  = $sysTeacherSalaryData['integral'];
				if ($bookCourseData['user_status'] ==BOOK_USER_STATUS_2){
					$money = $money/2;
					$data['remark'] ='studentsDidnotCome';
				}
				$data['target_id'] =   $bookCourseData['id'];
				$data['add_admin'] = $adminName;
				$data['type'] = 'HOLIDAY_SEASON';
				$data['teacher_id']  = $bookCourseData['teacher_id'] ;
				$data['money'] = $money;
				$data['integral'] = $integral;
				$this->add($data);
			}
		}
	}
	/**
	 * 差评
	 */
	public function badReview(){
		
	}
	/**
	 * 投诉
	 */ 
	public function complaints(){
		
	}
}

?>